package com.econ.powercloud.ems.controller;

import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.econ.powercloud.common.base.facade.pojo.PageResult;
import com.econ.powercloud.ems.pojo.*;
import com.econ.powercloud.ems.pojo.requestVO.EmsHisDataRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.SvgDataRequestVO;
import com.econ.powercloud.ems.pojo.scene.OpenApiHisDataVO;
import com.econ.powercloud.ems.service.biz.DeviceService;
import com.econ.powercloud.ems.service.biz.HisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "api/v2/his")
@Api(tags =" 历史数据查询")
public class HisController extends ApiBaseController {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private HisService hisService;

    @GetMapping(value = "getDeviceYxDit")
    @ApiOperation(value="历史遥信数据曲线模糊查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", value = "设备编码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "statusType", value = "遥信数据类型",dataType = "Integer"),
            @ApiImplicitParam(name = "remark", value = "说明",dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "行", required = true, dataType = "Integer")
    })
    public MsgResult<PageResult<List<EmsHisDataVO>>> getDeviceDitList(@RequestParam("deviceCode")String deviceCode,
                                                                      @RequestParam(value = "statusType",required = false)Integer statusType,
                                                                      @RequestParam(value = "remark",required = false)String remark,
                                                                      @RequestParam("pageNo")Integer pageNo,
                                                                      @RequestParam("pageSize")Integer pageSize){
        return successHandler(deviceService.queryYxListByDeviceCodeAndTypeAndRemark(deviceCode, statusType, remark, pageNo, pageSize));
    }

    @PostMapping(value = "getYcHisDataList")
    @ApiOperation(value="查询遥测点号历史数据")
    public MsgResult<Map<String,List<OpenApiHisDataVO>>> getYcHisData(@RequestBody EmsHisDataRequestVO<HisDataVO> requestVO){
        return successHandler(hisService.getYcHisData(requestVO));
    }

    @PostMapping(value = "getSvgSectionData")
    @ApiOperation(value="SVG断面数据")
    public MsgResult<List<OpenApiHisDataVO>> getSvgSectionData(@RequestBody @Validated SvgDataRequestVO requestVO){
        return successHandler(hisService.getSvgSectionData(requestVO.getStartTime(),requestVO.getJsonData()));
    }

    @PostMapping(value = "getSvgHisData")
    @ApiOperation(value="SVG历史数据")
    public MsgResult<Map<Long,List<OpenApiHisDataVO>>> getSvgHisData(@RequestBody @Validated SvgDataRequestVO requestVO){
        return successHandler(hisService.getSvgHisData(requestVO.getStartTime(),requestVO.getEndTime(),requestVO.getJsonData()));
    }

    @PostMapping(value = "getData")
    @ApiOperation(value="查询点号历史数据")
    public MsgResult<List<DitHisDataVO>> getData(@RequestBody EmsHisDataRequestVO<DitDataVO> requestVO){
        return successHandler(hisService.getHisDataByDitDataList(requestVO.getStartTime(),requestVO.getEndTime(),requestVO.getTimeIntervalType(),requestVO.getHisDataVOS()));
    }

    @PostMapping(value = "getTimeSeparation")
    @ApiOperation(value = "获取有历史数据的时间段")
    public MsgResult<List<TimeSeparation>> getTimeSeparation(@RequestBody EmsHisDataRequestVO<DitDataVO> requestVO){
        return successHandler(hisService.getTimeSeparation(requestVO.getStartTime(),requestVO.getEndTime(),
                requestVO.getHisDataVOS(),4 * 3600 * 1000L));
    }

}
